package com.bw.car.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bw.car.databinding.ItemCarCodeBinding
import com.bw.car.inter.Click

class CarCodeAdapter(private val list:MutableList<String>):RecyclerView.Adapter<CarCodeAdapter.CarCodeViewHolder>() {


    private var onclick:Click<String> ?= null

    fun setItemClick(listener:Click<String>){
        this.onclick=listener
    }

    inner class CarCodeViewHolder(val binding:ItemCarCodeBinding):RecyclerView.ViewHolder(binding.root)


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CarCodeViewHolder {
        val binding=ItemCarCodeBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return CarCodeViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: CarCodeViewHolder, position: Int) {
        val item=list[position]

        holder.binding.code.text=item

        holder.itemView.setOnClickListener {
            this.onclick?.onCLick(item,position)
        }
    }
}